//===============================================================================
// Microsoft patterns & practices
// Guidance Explorer Offline Client
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================



using GuidanceExplorer.Controls;

namespace GuidanceExplorer.Model
{
    public class ModelItemMenuItemIsEditableCondition : IMenuItemCondition
    {
        #region IMenuItemCondition Members

        public bool ShowMenuItem(MenuItemAttribute attribute, IModelItem modelItem)
        {
            if (modelItem == null)
            {
                return true;
            }
            else
            {
                GuidanceTypeNode typeNode = modelItem as GuidanceTypeNode;
                GuidanceItem guidanceItem = modelItem as GuidanceItem;
                if (typeNode != null)
                {
                    LibraryNode libNode = typeNode.Parent as LibraryNode;
                    if (libNode != null)
                    {
                        return libNode.ModelInstance.ReadProtection == ReadProtection.ReadWrite;
                    }
                }
                else if (guidanceItem != null && guidanceItem.Library != null)
                {
                    return guidanceItem.Library.ReadProtection == ReadProtection.ReadWrite;
                }
                /*else if (modelItem is GuidanceItemNode)
                {
                    Library library = ((GuidanceExplorer.Model.GuidanceItem)(((GuidanceExplorer.Controls.ModelNode<GuidanceExplorer.Model.GuidanceItemFileReference>)(modelItem)).Parent.)).Library;
                    return ((library == null) || (library.ReadProtection == ReadProtection.ReadWrite));
                }*/
                // if the GuidanceItem is not under a Library it is editable. 
				return modelItem.IsEditable;
            }
        }

        #endregion
    }
}

